angular.module('donation.controller', []).controller('donationController', ['$scope', '$http',"$charityProjectService", function ($scope , $http, $charityProjectService) {
    $scope.moneySelected = 100;
    $scope.customMoney = 50;
    $scope.companyFlag = false;
    $scope.currentUser = ($.context.session && $.context.session.user) || {};
    $scope.selectedProject = {};

    $scope.projects = [];
    $charityProjectService.getProjectList({},function(result){
        var projects = [];
        var projectData = {}
        _.forEach(result.data, function (project) {
            if(project.level == 1){
                projects.push(project);
            }
            else if(project.level == 2 && project.parent ){//project.level ==2
                if(projectData[project.parent+'']){
                    projectData[project.parent+''].push(project);
                }
                else{
                    projectData[project.parent+''] = [];
                    projectData[project.parent+''].push(project);
                }
            }
        });

        _.forEach(projects, function(_project){
            if(projectData[_project.id+'']){
                _project.children = projectData[_project.id+''];
            }
        });

        $scope.projects = projects;
        $scope.selectedProject = ($scope.projects && $scope.projects[0]) || {};
    });

    $scope.selectProjectItem = function (item) {
        $scope.selectedProject = item;
    };

    $scope.donate = {
        //对应捐赠项目ID
        projectId: $scope.selectedProject.id || '-1',
        projectName: $scope.selectedProject.name || '顺义慈善',
        totalFee: 10000,
        payType: 'WX',//('WX' 'ALi')
        userInfo: {
            id: $scope.currentUser.id,
            name: '',
            phoneNumber: $scope.currentUser.phoneNumber && parseInt($scope.currentUser.phoneNumber),
            email: $scope.currentUser.email,
            userType: 0,
            company:''
        },
        publicFlag: 1,//(公开: 1, 不公开:0)
    };
    //判断提交错误
    $scope.customMoneyError = 0;
    $scope.customMoneyErrorMessage = '';
    $scope.nameError = 0;
    $scope.nameErrorMessage = '';
    $scope.companyNameError = 0;
    $scope.companyNameErrorMessage = '';
    $scope.phoneError = 0;
    $scope.phoneErrorMessage = '';
    $scope.emailError = 0;
    $scope.emailErrorMessage = '';

    $scope.selectMoney = function (money) {
        $scope.moneySelected = money;
    };

    $scope.selectPayType = function (payType) {
        $scope.donate.payType = payType;
    };

    $scope.showCompany = function () {
        if($scope.donate.userInfo.userType == 2){
            $scope.companyFlag = true;
        }else{
            $scope.companyFlag = false;
            $scope.donate.userInfo.company = '';
        }
    };

    $scope.toDonate = function (event) {
        $scope.donate.projectId = $scope.selectedProject.id || '-1';
        $scope.donate.projectName = $scope.selectedProject.name || '顺义慈善';

        //TODO 测试数据 check elements;
        //TODO 后台所需数据如下
        //用户自己填写的金额
        if($scope.moneySelected == -1){
            if(!verifyMoney($scope.customMoney)  || parseFloat($scope.customMoney) <= 0){
                $scope.customMoneyError = 1;
                $scope.customMoneyErrorMessage = '请填写正确的捐款金额';
                event.preventDefault();
                return;
            }else{
                $scope.donate.totalFee = parseFloat($scope.customMoney);
            }
        }
        else {
            $scope.donate.totalFee = $scope.moneySelected;
        }

        if($scope.donate.totalFee){

        }

        //验证用户姓名
        if(!$scope.donate.userInfo.name){
            $scope.nameError = 1;
            $scope.nameErrorMessage = '请填写捐款人姓名';
            event.preventDefault();
            return;
        }

        //验证用户公司姓名格式
        if($scope.donate.userInfo.userType == 2 && !$scope.donate.userInfo.company){
            $scope.companyNameError = 1;
            $scope.companyNameErrorMessage = '请填写捐款人公司名';
            event.preventDefault();
            return;
        }

        // 验证用户手机号
        if(!verifyPhoneNum($scope.donate.userInfo.phoneNumber)){
            $scope.phoneError = 1;
            $scope.phoneErrorMessage = '请填写正确的手机号码';
            event.preventDefault();
            return;
        }
        //验证邮箱格式
        if(!verifyEmail($scope.donate.userInfo.email)){
            $scope.emailError = 1;
            $scope.emailErrorMessage = '请填写正确的电子邮箱';
            console.log($scope.donate.userInfo.email);
            event.preventDefault();
            return;
        }
        //debugger;
        //阻止提交
        //  event.preventDefault();
    };


    //验证用户自己填写金额
    $scope.customMoneyErr = function () {
        if($scope.moneySelected == -1) {

            if (!verifyMoney($scope.customMoney)  || parseFloat($scope.customMoney) <= 0 ) {
                $scope.customMoneyError = 1;
                $scope.customMoneyErrorMessage = '请填写正确的捐款金额';
                event.preventDefault();
                return;
            }else{
                $scope.customMoneyError = 0;
            }
        }
    };

    //验证用户姓名
    $scope.nameErr = function(){
        // debugger;
        if(!$scope.donate.userInfo.name){
            $scope.nameError = 1;
            $scope.nameErrorMessage = '请填写捐款人姓名';
            event.preventDefault();
            return;
        }else{
            $scope.nameError = 0;
        }
    };
    //验证用户公司姓名格式
    $scope.companyNameErr = function() {
        if ($scope.donate.userInfo.userType == 2 && !$scope.donate.userInfo.company) {
            $scope.companyNameError = 1;
            $scope.companyNameErrorMessage = '请填写捐款人公司名';
            event.preventDefault();
            return;
        }else{
            $scope.companyNameError = 0;
        }
    };

    // 验证用户手机号
    $scope.phoneErr = function() {
        if (!verifyPhoneNum($scope.donate.userInfo.phoneNumber)) {
            $scope.phoneError = 1;
            $scope.phoneErrorMessage = '请填写正确的手机号码';
            event.preventDefault();
            return;
        }else{
            $scope.phoneError = 0;
        }
    };
    //验证邮箱格式
    $scope.emailErr = function() {
        if (!verifyEmail($scope.donate.userInfo.email)) {
            $scope.emailError = 1;
            $scope.emailErrorMessage = '请填写正确的电子邮箱';
            event.preventDefault();
            return;
        }else{
            $scope.emailError = 0;
        }
    };
}]);